//
//  OtherUserCenterVC.m
//  danpin
//
//  Created by chuangjia on 4/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "OtherUserCenterVC.h"
#import "RootViewController.h"
#import "MEExpandableHeaderView.h"
#import "viewconfig.h"
#import "CellConfig.h"
#import "BaseModel.h"
#import "HTHorizontalSelectionList.h"
#import "GoodViewController.h"
#import "CommentViewController.h"
#import "ZToastManager.h"
#import "NSString+Extensions.h"
#import "GoodConfig.h"
#import "ShareorderConfig.h"
#import "HcCustomKeyboard.h"
#import "UMengShare.h"
#import "WebViewController.h"
#import "UserInfo.h"
@interface OtherUserCenterVC ()<UITableViewDataSource,UITableViewDelegate,HTHorizontalSelectionListDataSource,HTHorizontalSelectionListDelegate,UIAlertViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,HcCustomKeyboardDelegate>
{
    
}
@property(strong,nonatomic) UITableView *tbView;
@property(strong,nonatomic) NSMutableArray *dataArray;
@property(strong,nonatomic) NSMutableArray *tabArray;
@property(strong,nonatomic) MEExpandableHeaderView *headerView;
@property(nonatomic,strong) NSString *action;
@property(nonatomic,strong) NSString *userId;
@property(strong,nonatomic) UIImageView *iconView;
//
@property(nonatomic,assign)int currentIndex;
@property(nonatomic,strong)HTHorizontalSelectionList *selectionList;
//
@property(nonatomic,assign)int currentPage;
@property(nonatomic,assign)BOOL isLoadFinished;
//
@property(nonatomic,strong)UIButton *followBtn;
@property(strong,nonatomic) UILabel *fansLabel;
@property(strong,nonatomic) UILabel *levelLabel;
@end

@implementation OtherUserCenterVC
@synthesize userDictionary=_userDictionary;
@synthesize followBtn=_followBtn;
@synthesize fansLabel=_fansLabel;
@synthesize levelLabel=_levelLabel;
- (id)init
{
    self = [super init];
    if (self)
    {
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
#endif
    [UMengShare event:CK_USER acc:_currentIndex];
    // Do any additional setup after loading the view.
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc] init];
    }
    if (!_tabArray) {
        _tabArray =[[NSMutableArray alloc] init];
    }
    _tabArray = [NSMutableArray arrayWithObjects:@"晒单", @"喜欢", @"评论",@"关注",@"粉丝",nil,nil];
    
    _currentIndex =0;
    _currentPage=0;
    _isLoadFinished=NO;
    _action =@"shaidan";

     self.userId  = [[NSString stringWithFormat:@"%@",[self.userDictionary objectForKey:@"user_id"]] replaceNullString];
    [self initUI];
    
    _currentIndex =(int)index;
    [self.dataArray removeAllObjects];
    self.currentPage=0;
 //   [ZShare showAnimationView:self.view center:CGPointZero];
    [self getUserDataWithAction:_action];
}

-(void)dealloc
{
    self.followBtn=nil;
    self.fansLabel=nil;
    self.levelLabel=nil;
    
    self.tabArray =nil;
    self.dataArray=nil;
    
    self.userDictionary=nil;
    if (self.selectionList) {
        self.selectionList.dataSource=nil;
        self.selectionList.delegate=nil;
        [self.selectionList removeFromSuperview];
        self.selectionList=nil;
    }
    if (_tbView) {
        _tbView.dataSource=nil;
        _tbView.delegate=nil;
        [_tbView removeFromSuperview];
        _tbView= nil;
    }
    if(self.headerView)
    {
        UIView* currentView = [_headerView viewWithTag:1000000];
        [currentView removeFromSuperview];
        currentView=nil;
        [self.headerView removeFromSuperview];
        self.headerView=nil;
    }
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)reloadData
{
    if (self.selectionList) {
        [self selectionList:_selectionList didSelectButtonWithIndex:_currentIndex];
    }
}
//
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor =[UIColor clearColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tbView];
    //
    if(self.headerView)
    {
        UIView* currentView = [_headerView viewWithTag:1000000];
        [currentView removeFromSuperview];
        currentView=nil;
        [self.headerView removeFromSuperview];
        self.headerView=nil;
    }
    CGSize headerViewSize = CGSizeMake(self.view.bounds.size.width, hightAuto(233));
    _headerView = [[MEExpandableHeaderView alloc] initWithSize:headerViewSize
                                                                      backgroundImage:[UIImage imageNamed:@"my_background.png"]
                                                                         contentViews:[self tableheadView]];
    self.tbView.tableHeaderView = _headerView;
    //
    
    __weak typeof (self) wSelf = self;
    [self.tbView addInfiniteScrollingWithActionHandler:^{
        
        if (wSelf.isLoadFinished) {
            //停止菊花
            [wSelf.tbView.infiniteScrollingView stopAnimating];
            [wSelf.tbView setShowsInfiniteScrolling:NO];
            return;
        }
        [wSelf.tbView.infiniteScrollingView startAnimating];
        wSelf.currentPage=(int)[wSelf.dataArray count]+1;
        [wSelf getUserDataWithAction:wSelf.action];
    }];
    
    
}
#pragma mark - tableview
#pragma mark - tableview
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_action isEqualToString:@"shaidan"]) {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        return [CategoryListCell returnCellHeight:dict];
    }else if ([_action isEqualToString:@"like"]) {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        return [CategoryListCell returnCellHeight:dict];
    }else if ([_action isEqualToString:@"comment"]) {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        return [UserSayCell returnCellHeight:dict];
    }else if ([_action isEqualToString:@"follow"]) {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        return [UserFollowCell returnCellHeight:dict];
    }else if ([_action isEqualToString:@"fans"]) {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        return [UserFollowCell returnCellHeight:dict];
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return hightAuto(40);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, hightAuto(40))];
    sectionView.backgroundColor=[UIColor clearColor];
    if (!_selectionList) {
        _selectionList = [[HTHorizontalSelectionList alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, hightAuto(40))];
        _selectionList.delegate = self;
        _selectionList.dataSource = self;
        _selectionList.selectionIndicatorColor=UIColorFromRGB(0xd21d1d);
        _selectionList.bottomTrimColor =UIColorFromRGB(0xc7c8c8);
    }
    [sectionView addSubview:_selectionList];
    return sectionView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = hightAuto(40);
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
    if (scrollView == self.tbView)
    {
        [self.headerView offsetDidUpdate:scrollView.contentOffset];
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@""];
    if ([_action isEqualToString:@"shaidan"]) {
        CategoryListCell *cell = (CategoryListCell*)[tableView dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor =[UIColor whiteColor];
            _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
            
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        [cell showInfo:dict key:@"shaidan" indexPath:indexPath];
        return cell;
    }else if ([_action isEqualToString:@"like"]) {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        
            CategoryListCell *cell = (CategoryListCell*)[tableView dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[CategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                cell.backgroundColor =[UIColor whiteColor];
                _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
                
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            
            [cell showInfo:dict key:@"shaidan" indexPath:indexPath];
            return cell;
        
    }else if ([_action isEqualToString:@"comment"]) {
        
        UserSayCell *cell = (UserSayCell*)[tableView dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[UserSayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
            cell.backgroundColor =[UIColor whiteColor];
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        [cell showInfo:dict key:@"comment" indexPath:indexPath];
        return cell;
    }else if ([_action isEqualToString:@"follow"]) {
        
        UserFollowCell *cell = (UserFollowCell*)[tableView dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[UserFollowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
            cell.backgroundColor =[UIColor whiteColor];
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        [cell showInfo:dict key:@"follow" indexPath:indexPath];
        return cell;
    }else if ([_action isEqualToString:@"fans"]) {
        
        UserFollowCell *cell = (UserFollowCell*)[tableView dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[UserFollowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
            cell.backgroundColor =[UIColor whiteColor];
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        [cell showInfo:dict key:@"follow" indexPath:indexPath];
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row>=self.dataArray.count) {
        return;
    }
    
    if ([_action isEqualToString:@"shaidan"]||[_action isEqualToString:@"like"])
    {
        NSDictionary *dict =[self.dataArray objectAtIndex:indexPath.row];
        int is_shaidan =[[dict objectForKey:@"is_shaidan"] intValue];
        if (is_shaidan==1)
        {
            ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
            good.type =-1;
            good.dictionary = dict;
            good.photo_id = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]] replaceNullString];
            [self.navigationController pushViewController:good animated:YES];
            good=nil;
        }else if (is_shaidan==20) {
                WebViewController *web =[[WebViewController alloc] init];
                web.titlestr=@"otheruser";
                [self.navigationController pushViewController:web animated:YES];
                web=nil;
                
            }else{
                GoodViewController *good =[[GoodViewController alloc] init];
                good.type =-1;
                good.dictionary =dict;
                good.photo_id = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]] replaceNullString];
                [self.navigationController pushViewController:good animated:YES];
            }
        
    }else if ([self.action isEqualToString:@"comment"]) {
        NSDictionary *data =[self.dataArray objectAtIndex:indexPath.row];
        NSDictionary *ware =[data objectForKey:@"ware"];
        int isShaidan = [[ware objectForKey:@"is_shaidan"] intValue];
        if (isShaidan==1) {
            ShaiorderDetailViewController *good =[[ShaiorderDetailViewController alloc] init];
            good.type =-1;
            good.dictionary = ware;
            good.photo_id = [[NSString stringWithFormat:@"%@",[ware objectForKey:@"photo_id"]] replaceNullString];
            [self.navigationController pushViewController:good animated:YES];
            good=nil;
        }else
        {
            GoodViewController *good =[[GoodViewController alloc] init];
            good.type =-1;
            good.dictionary =ware;
            good.photo_id = [[NSString stringWithFormat:@"%@",[ware objectForKey:@"photo_id"]] replaceNullString];
            [self.navigationController pushViewController:good animated:YES];
        }
        
    }else if ([self.action isEqualToString:@"follow"]||[self.action isEqualToString:@"fans"]) {
        NSDictionary *data =[self.dataArray objectAtIndex:indexPath.row];
        OtherUserCenterVC *user =[[OtherUserCenterVC alloc] init];
        NSMutableDictionary *dict =[NSMutableDictionary dictionary];
        NSString *largeFace = [[NSString stringWithFormat:@"%@",[data objectForKey:@"largeFace"]] replaceNullString];
        [dict setValue:largeFace forKey:@"largeFace"];
        if ([self.action isEqualToString:@"fans"]) {
            
            NSString *fans_id = [[NSString stringWithFormat:@"%@",[data objectForKey:@"fans_id"]] replaceNullString];
            [dict setValue:fans_id forKey:@"user_id"];
        }else{
            
            NSString *follow_id = [[NSString stringWithFormat:@"%@",[data objectForKey:@"follow_id"]] replaceNullString];
            [dict setValue:follow_id forKey:@"user_id"];
        }
        
        NSString *nick_name = [[NSString stringWithFormat:@"%@",[data objectForKey:@"nick_name"]] replaceNullString];
        [dict setValue:nick_name forKey:@"nick_name"];
        user.userDictionary=dict;
        [self.navigationController pushViewController:user animated:YES];
        
    }
}
-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath *)indexPath
{
    switch (type) {
        case BEventType_comment:
        {
            NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            CommentViewController *comm =[[CommentViewController alloc] init];
            comm.photo_id =pid;
            comm.dictionary =obj;
            [self.navigationController pushViewController:comm animated:YES];
        }
            break;
        case BEventType_like:
        {
            NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            if ([self isLogin]) {
                [self likeWithPid:pid indexPath:indexPath];
            }
        }
            break;
            
        default:
            break;
    }
}
//喜欢
-(void)likeWithPid:(NSString*)pid  indexPath:(NSIndexPath*)indexPath
{
    [UMengShare event:CK_LOVE acc:1];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:pid forKey:@"photo_id"];
    [BaseModel love:dict success:^(AFHTTPRequestOperation *operation, id result) {
        int code =  [[result objectForKey:@"code"] intValue];
        NSString *like_cnt =  [NSString stringWithFormat:@"%@",[result objectForKey:@"like_cnt"]];
        if (code==1 || code==2) {
            for (int i=0; i<[wSelf.dataArray count]; i++) {
                NSDictionary *dict =[wSelf.dataArray objectAtIndex:i];
                if ([dict isKindOfClass:[NSDictionary class]]) {
                    NSString *pidd =[NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                    if ([pidd isEqualToString:pid]) {
                        NSMutableDictionary *temp = [dict mutableCopy];
                        if (code==1) {
                            [temp setValue:@"1" forKey:@"like"];
                        }else{
                            [temp setValue:@"0" forKey:@"like"];
                        }
                        [temp setValue:like_cnt forKey:@"like_cnt"];
                        [wSelf.dataArray replaceObjectAtIndex:i withObject:temp];
                        break;
                    }
                }
            }
            [self.tbView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        }
        
    } failure:^(NSError *error) {
   //     [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}

//
//
-(void)getUserDataWithAction:(NSString*)action
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:action forKey:@"action"];
    [dict setValue:[[NSString stringWithFormat:@"%@",self.userId] replaceNullString] forKey:@"user_id"];
    [dict setValue:[[NSString stringWithFormat:@"%@",[self.userDictionary objectForKey:@"nick_name"]] replaceNullString] forKey:@"user_name"];
    [dict setValue:[[NSString stringWithFormat:@"%d",self.currentPage] replaceNullString] forKey:@"from"];
    __weak typeof (self) wSelf = self;
    BOOL isFetchCache = NO;
    if (self.currentPage==0) {
        isFetchCache=YES;
    }else{
        isFetchCache=NO;
    }
    [UserInfo homeV1:dict allowFetchCache:isFetchCache success:^(AFHTTPRequestOperation *operation, id result) {
        [wSelf parserResultObject:result];
        [wSelf.tbView.infiniteScrollingView stopAnimating];
    //    [ZShare hiddenAnimationView:wSelf.view];
    } failure:^(NSError *error) {
        [wSelf.tbView.infiniteScrollingView stopAnimating];
      //  [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}
-(void)parserResultObject:(id)resultObject
{
    [[ZToastManager ShardInstance] hideprogress];
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        
        id user = [resultObject objectForKey:@"user"];
        if (user && [user isKindOfClass:[NSDictionary class]]) {
            self.userDictionary = user;
            self.userId = [[NSString stringWithFormat:@"%@",[self.userDictionary objectForKey:@"user_id"]] replaceNullString];
            //
            NSString *fanscount =[[NSString stringWithFormat:@"%@",[self.userDictionary objectForKey:@"fans_count"]] replaceNullString];
            NSString *followscount =[[NSString stringWithFormat:@"%@",[self.userDictionary objectForKey:@"follow_count"]]replaceNullString];
            NSString *level =[[NSString stringWithFormat:@"%@",[self.userDictionary objectForKey:@"user_level"]]replaceNullString];
            if (level.length==0) {
                level=@"0";
            }
            self.levelLabel.text =[NSString stringWithFormat:@"%@级",level];
            if (followscount.length==0) {
                followscount = @"0";
            }
            if (fanscount.length==0) {
                fanscount = @"0";
            }
            self.fansLabel.text =[NSString stringWithFormat:@"关注 %@ | 粉丝 %@",followscount,fanscount];
            //
            int follow =[[user objectForKey:@"followed"] intValue];
            if (follow==2) {
                _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
                [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
                [_followBtn setTitle:@"互相关注" forState:UIControlStateNormal];
            }else
            if (follow==1) {
                _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
                [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
                [_followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
            }else if (follow==0){
                _followBtn.backgroundColor =UIColorFromRGB(0x41c339);
                [_followBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
                [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
            }
            NSString *middleFace = [[self.userDictionary objectForKey:@"largeFace"] replaceNullString];
            if (middleFace.length>0) {
                [_iconView sd_setImageWithURL:[NSURL URLWithString:middleFace] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
            }
        
        }
        id data =  [resultObject objectForKey:@"data"];
        if (data && [data isKindOfClass:[NSArray class]]){
            if (self.currentPage==0) {
                [self.dataArray removeAllObjects];
            }
            for (id dict in data) {
                [self.dataArray addObject:dict];
            }
        }
        id no_data =  [resultObject objectForKey:@"no_data"];
        if (no_data&&[no_data isKindOfClass:[NSString class]]) {
       //     [ZShare hiddenAnimationView:self.view text:no_data];
            self.tbView.tableFooterView =nil;
        }else {
      //      [ZShare hiddenAnimationView:self.view];
        }
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
        
        if (self.isLoadFinished) {
            
            NSString *end_msg =[resultObject objectForKey:@"end_msg"];
            self.tbView.tableFooterView =[ZShare footerView:end_msg];
            
            [self.tbView.infiniteScrollingView stopAnimating];
            [self.tbView setShowsInfiniteScrolling:NO];
        }
    }
    [self.tbView reloadData];
    
}
#pragma mark - UIScrollViewDelegate

//
-(UIView*)tableheadView
{
    UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(233))];
    bgView.backgroundColor =[UIColor clearColor];
    bgView.tag =1000000;
    //
    UIButton *backBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.backgroundColor =[UIColor clearColor];
    backBtn.tag=0;
    backBtn.frame=CGRectMake(0, 20, widthAuto(31), hightAuto(31));
    backBtn.titleLabel.font =FONT(13);
    [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(settingbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:backBtn];
    //
    UIButton *settingBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    settingBtn.backgroundColor =[UIColor clearColor];
    settingBtn.tag=1;
    settingBtn.frame=CGRectMake(DEVICE_WIDTH -PHOTO_FRAME_WIDTH- widthAuto(31), PHOTO_FRAME_WIDTH*2+4, widthAuto(31), hightAuto(31));
    settingBtn.titleLabel.font =FONT(13);
    [settingBtn setImage:[UIImage imageNamed:@"message.png"] forState:UIControlStateNormal];
    [settingBtn addTarget:self action:@selector(settingbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:settingBtn];
    //
    _iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
    _iconView.frame = CGRectMake((DEVICE_WIDTH-widthAuto(60))/2,YAuto(60), widthAuto(60), widthAuto(60));
    _iconView.layer.cornerRadius=widthAuto(30);
    _iconView.layer.masksToBounds = YES;
    _iconView.userInteractionEnabled =YES;
    
    NSString *middleFace = [[self.userDictionary objectForKey:@"largeFace"] replaceNullString];
    [_iconView sd_setImageWithURL:[NSURL URLWithString:middleFace] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
    [bgView addSubview:_iconView];
   
    //
    _levelLabel =[[UILabel alloc] initWithFrame:CGRectZero];
    _levelLabel.frame = CGRectMake(_iconView.frame.origin.x+_iconView.frame.size.width/1.5,_iconView.frame.origin.y+_iconView.frame.size.height-PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*3, PHOTO_FRAME_WIDTH*2);
    _levelLabel.backgroundColor = [UIColor blackColor];
    _levelLabel.textColor = [UIColor whiteColor];
    _levelLabel.textAlignment = NSTextAlignmentCenter;
    _levelLabel.layer.cornerRadius =PHOTO_FRAME_WIDTH;
    _levelLabel.layer.masksToBounds=YES;
    _levelLabel.font = FONT(10);
    _levelLabel.text =@"0级";
    _levelLabel.userInteractionEnabled =YES;
    [bgView addSubview:_levelLabel];
    
    //
    UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectZero];
    nameLable.frame = CGRectMake(0,_iconView.frame.origin.y+_iconView.frame.size.height+PHOTO_FRAME_WIDTH, DEVICE_WIDTH, PHOTO_FRAME_WIDTH*2);
    nameLable.backgroundColor = [UIColor clearColor];
    nameLable.textColor = [UIColor blackColor];
    nameLable.textAlignment = NSTextAlignmentCenter;
    nameLable.font = [UIFont boldSystemFontOfSize:14];
    nameLable.layer.shadowColor = [UIColor whiteColor].CGColor;
    nameLable.layer.shadowOffset = CGSizeMake(1,1);
    nameLable.layer.shadowOpacity = 0.3;
    nameLable.layer.shadowRadius = 0.3;
    nameLable.text =[self.userDictionary objectForKey:@"nick_name"];
    nameLable.userInteractionEnabled =YES;
    [bgView addSubview:nameLable];
    nameLable=nil;
    
    _fansLabel =[[UILabel alloc] initWithFrame:CGRectZero];
    _fansLabel.frame = CGRectMake(0,_iconView.frame.origin.y+_iconView.frame.size.height+PHOTO_FRAME_WIDTH*3.5, DEVICE_WIDTH, PHOTO_FRAME_WIDTH*2);
    _fansLabel.backgroundColor = [UIColor clearColor];
    _fansLabel.textColor = [UIColor darkGrayColor];
    _fansLabel.textAlignment = NSTextAlignmentCenter;
    _fansLabel.font = [UIFont boldSystemFontOfSize:12];
    _fansLabel.layer.shadowColor = [UIColor whiteColor].CGColor;
    _fansLabel.layer.shadowOffset = CGSizeMake(1,1);
    _fansLabel.layer.shadowOpacity = 0.3;
    _fansLabel.layer.shadowRadius = 0.3;

    _fansLabel.text =@"关注 0 | 粉丝 0";
    _fansLabel.userInteractionEnabled =YES;
    [bgView addSubview:_fansLabel];

    
    _followBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _followBtn.backgroundColor =UIColorFromRGB(0x41c339);
    _followBtn.tag=2;
    _followBtn.frame=CGRectMake((DEVICE_WIDTH -widthAuto(90))/2, _fansLabel.frame.origin.y+_fansLabel.frame.size.height+PHOTO_FRAME_WIDTH, widthAuto(90), hightAuto(33));
    _followBtn.titleLabel.font =FONT(13);
    _followBtn.layer.cornerRadius=3;
    [_followBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
    [_followBtn addTarget:self action:@selector(settingbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_followBtn];
    
    return bgView;
}
//
-(void)settingbtnClick:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
            break;
        case 1:
        {
            [self showCommentViewWithData];
        }
            break;
        case 2:
        {
            if ([self isLogin]) {
                //            __weak typeof (self) wSelf = self;
                NSMutableDictionary *dict =[NSMutableDictionary dictionary];
                [dict setValue:@"follow_user" forKey:@"action"];
                [dict setValue:@"" forKey:@"brand_id"];
                [dict setValue:@"" forKey:@"cate_id"];
                [dict setValue:self.userId forKey:@"follow_id"];
                [dict setValue:@"" forKey:@"tag"];
                
                [dict setValue:[self.userDictionary objectForKey:@"nick_name"] forKey:@"follow_name"];
                
                
                [UserInfo follow:dict success:^(AFHTTPRequestOperation* operation, id result){
                    
                    if (result&&[result isKindOfClass:[NSDictionary class]]) {
                        NSDictionary *resultDist =(NSDictionary*)result;
                        [[ZToastManager ShardInstance] showtoast:[resultDist objectForKey:@"msg"]];
                        if ([[resultDist objectForKey:@"code"] intValue]==1) {
                            _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
                            [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
                            [_followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
                        }else
                        {
                            _followBtn.backgroundColor =UIColorFromRGB(0x41c339);
                            [_followBtn setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
                            [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
                        }
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:nil];
                    }
                    
                } failure:^(NSError* error){
                    [[ZToastManager ShardInstance] showtoast:@"关注失败"];
                }];
            }

        }
            break;
        default:
            break;
    }
    
}
#pragma mark - HTHorizontalSelectionListDataSource Protocol Methods

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.tabArray.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    if (index>=self.tabArray.count) {
        return @"";
    }
    NSString *kstr =[self.tabArray objectAtIndex:index];
    return kstr;
}
#pragma mark - HTHorizontalSelectionListDelegate Protocol Methods

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    switch (index) {
//        case 0:// @"求购",
//        {
//            _action=@"asking";
//        }
//            break;
//        
        case 0://@"晒单",
        {
            _action=@"shaidan";
        }
            break;
        case 1:// @"喜欢"
        {
            _action=@"like";
        }
            break;
        case 2:// @"评论"
        {
            _action=@"comment";
        }
            break;
        case 3:// @"关注"
        {
            _action=@"follow";
        }
            break;
        case 4:// @"关注"
        {
            _action=@"fans";
        }
            break;
        default:
            break;
    }
    _currentIndex =(int)index;
    [self.tbView setShowsInfiniteScrolling:YES];
    self.tbView.tableFooterView =nil;
    [self.dataArray removeAllObjects];
    [self.tbView reloadData];
    self.currentPage=0;
 //   [ZShare showAnimationView:self.view center:CGPointZero];
    [self getUserDataWithAction:_action];
}
//评论
-(void)showCommentViewWithData
{
    [[HcCustomKeyboard customKeyboard] textViewShowView:self customKeyboardDelegate:self];
     [[HcCustomKeyboard customKeyboard].mTextView  becomeFirstResponder];
}
-(void)hideView
{
    [self backBtnClick];
}
-(void)backBtnClick
{
    [[HcCustomKeyboard customKeyboard] textViewShowView:nil customKeyboardDelegate:nil];
    [HcCustomKeyboard customKeyboard].mDelegate=nil;
    [[HcCustomKeyboard customKeyboard].mTextView resignFirstResponder];
    [[HcCustomKeyboard customKeyboard].mTextView removeFromSuperview];
    [HcCustomKeyboard customKeyboard].mTextView=nil;
}
-(void)talkBtnClick:(UITextView *)textViewGet
{
    if(textViewGet.text.length==0)
    {
        [[ZToastManager ShardInstance] showtoast:@"请输入内容"];
        return;
    }
    [self backBtnClick];
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:self.userId forKey:@"user_id"];
    [dict setValue:[self.userDictionary objectForKey:@"nick_name"] forKey:@"user_name"];
    
    [dict setValue:textViewGet.text forKey:@"text"];
    [BaseModel chat:dict allowFetchCache:NO success:^(AFHTTPRequestOperation *operation, id result) {
        NSDictionary *resultObject =(NSDictionary*)result;
        [[ZToastManager ShardInstance] showtoast:[resultObject objectForKey:@"msg"]];
    } failure:^(NSError *error) {
        [[ZToastManager ShardInstance] showtoast:@"点评失败"];
    }];
}

@end
